package com.pkutech.carcasher.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.TextView;

import com.pkutech.carcasher.R;
import com.pkutech.carcasher.bean.LeftParking;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 剩余车位
 * Created by cyy on 2016/6/9.
 */
public class LeftParkingActivity extends BaseActivity {

    public static final String CAR_NUM = "CAR_NUM";

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.gridview)
    GridView gridview;

    List<LeftParking> leftParkingList;
    ParkingAdapter parkingAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_parking_left);
        ButterKnife.bind(this);
        initToolbar(toolbar);

        initData();

    }

    public void initData(){
        leftParkingList = new ArrayList<LeftParking>();
        for(int i = 0; i < 10; i++){
            LeftParking lp = new LeftParking();
            lp.setNum("A00"+i);
            leftParkingList.add(lp);
        }
        parkingAdapter = new ParkingAdapter(this, leftParkingList);
        gridview.setAdapter(parkingAdapter);
        gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(LeftParkingActivity.this, DoParkingOrderActivity.class);
                intent.putExtra(CAR_NUM, leftParkingList.get(position).getNum());
                startActivity(intent);
            }
        });
    }

    public class ParkingAdapter extends BaseAdapter {

        private Context context;
        private List<LeftParking> leftParkings;
        private LayoutInflater inflater; // 视图容器

        public ParkingAdapter(Context context, List<LeftParking> leftParkings) {
            this.context = context;
            this.leftParkings = leftParkings;
            this.inflater = LayoutInflater.from(context); // 创建视图容
        }

        @Override
        public View getView(int position, View view, ViewGroup parent) {
            ViewHolder holder;
            if (view != null) {
                holder = (ViewHolder) view.getTag();
            } else {
                view = inflater.inflate(R.layout.left_parking_item, parent, false);
                holder = new ViewHolder(view);
                view.setTag(holder);
            }

            LeftParking leftParking = leftParkings.get(position);
            holder.parkingNum.setText(leftParking.getNum());

            return view;
        }

        @Override
        public int getCount() {
            return leftParkings.size();
        }

        @Override
        public Object getItem(int position) {
            return leftParkings.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        class ViewHolder {
            @BindView(R.id.parkingNum)
            TextView parkingNum;

            ViewHolder(View view) {
                ButterKnife.bind(this, view);
            }
        }
    }

}
